let WebIM = require('../../../../../utils/WebIM')['default']
let msgType = require('../../../msgtype')
import { $event } from '../../../../../utils'
Component({
  properties: {
    username: {
      type: Object,
      value: {},
    },
    chatType: {
      type: String,
      value: msgType.chatType.SINGLE_CHAT,
    },
  },
  data: {
    inputMessage: '', // render input 的值
    userMessage: '', // input 的实时值
    isShow: false,
  },
  lifetimes: {
    attached: function () {
      setTimeout(() => {
        if (this.properties.username.shareObj) {
          this.shareMsg()
        }
      }, 1000)
    },
  },
  methods: {
    showBottom() {
      var isShow = this.data.isShow
      this.setData({
        isShow: !isShow,
      })
      this.triggerEvent('parentEvent', null, {})
    },
    toggleRecordModal() {
      this.triggerEvent('tapSendAudio', null, {
        bubbles: true,
        composed: true,
      })
    },
    focus(e) {
      const { height } = e.detail
      this.data.isShow ? this.showBottom() : ''
      this.triggerEvent('inputFocused', null, { bubbles: true })
      $event.emit('inputEvent', height)
    },

    blur() {
      this.triggerEvent('inputBlured', null, { bubbles: true })
      $event.emit('inputEvent', 0)
    },

    isGroupChat() {
      return this.data.chatType == msgType.chatType.CHAT_ROOM
    },

    getSendToParam() {
      return this.isGroupChat() ? this.data.username.groupId : this.data.username.your
    },

    // bindinput 不能打冒号！
    bindMessage(e) {
      this.setData({
        userMessage: e.detail.value,
      })
    },

    emojiAction(emoji) {
      var str
      var msglen = this.data.userMessage.length - 1
      if (emoji && emoji != '[del]') {
        str = this.data.userMessage + emoji
      } else if (emoji == '[del]') {
        let start = this.data.userMessage.lastIndexOf('[')
        let end = this.data.userMessage.lastIndexOf(']')
        let len = end - start
        if (end != -1 && end == msglen && len >= 3 && len <= 4) {
          str = this.data.userMessage.slice(0, start)
        } else {
          str = this.data.userMessage.slice(0, msglen)
        }
      }
      this.setData({
        userMessage: str,
        inputMessage: str,
      })
    },
    shareMsg() {
      let me = this
      let id = WebIM.conn.getUniqueId()
      let msg = new WebIM.message(msgType.TEXT, id)
      msg.set({
        ext: this.properties.username.shareObj ? this.properties.username.shareObj : '',
        msg: '',
        from: this.data.username.myName,
        to: `${this.getSendToParam()}`,
        roomType: false,
        chatType: this.data.chatType,
        success(id, serverMsgId) {
          $event.emit('em.chat.sendSuccess', id, me.data.userMessage)
        },
        fail(id, serverMsgId) {
          console.log('失败了')
        },
      })
      if (this.data.chatType == msgType.chatType.CHAT_ROOM) {
        msg.setGroup('groupchat')
      }

      WebIM.conn.send(msg.body)
      this.properties.username.shareObj = {}
      this.triggerEvent(
        'newTextMsg',
        {
          msg: msg,
          type: msgType.TEXT,
        },
        {
          bubbles: true,
          composed: true,
        },
      )
      //
      // this.setData({
      //   userMessage: '',
      //   inputMessage: '',
      // })
    },
    sendMessage() {
      let me = this

      if (!this.data.userMessage.trim()) {
        return
      }

      let id = WebIM.conn.getUniqueId()
      let msg = new WebIM.message(msgType.TEXT, id)
      msg.set({
        msg: this.data.userMessage,
        from: this.data.username.myName,
        to: `${this.getSendToParam()}`,
        roomType: false,
        chatType: this.data.chatType,
        success(id, serverMsgId) {
          $event.emit('em.chat.sendSuccess', id, me.data.userMessage)
        },
        fail(id, serverMsgId) {},
      })
      if (this.data.chatType == msgType.chatType.CHAT_ROOM) {
        msg.setGroup('groupchat')
      }
      WebIM.conn.send(msg.body)
      this.triggerEvent(
        'newTextMsg',
        {
          msg: msg,
          type: msgType.TEXT,
        },
        {
          bubbles: true,
          composed: true,
        },
      )
      //
      this.setData({
        userMessage: '',
        inputMessage: '',
      })
    },
  },

  // lifetimes
  created() {},
  attached() {},
  moved() {},
  detached() {},
  ready() {},
})
